<?php
/**
 *
 * MongoDB 集合操作工具
 *
 * MongoDBCollect.php
 * Created by IntelliJ IDEA.
 *
 * @author: hjh <hjh@jf.com>
 *
 * Date: 2017/9/27
 * Time: 15:39
 */

use Common\MongoDbBase;

require dirname(__DIR__) . '/cronBase.php';

class MongoDBCollect extends cronBase
{
    private function usage($app)
    {
        fwrite(STDERR, "Usage:\n   {$app} [GetDataBases|RemoveDataBase|RemoveCollection] [DbName] [CollectionName]\n");
    }

    public function doDefault()
    {
        global $argv;
        $this->usage($argv[0]);
    }

    public function doGetDataBases()
    {
        $mongoBase = new MongoDbBase('test', 'test');
        foreach ($mongoBase->getDataBases() as $db) {
            var_dump($db);
        }
    }

    public function doRemoveDataBase()
    {
        global $argv;
        if (count($argv) < 3) {
            $this->usage($argv[0]);
            return;
        }
        $db = $argv[2];
        $collection = 'test';
        $mongoBase = new MongoDbBase($db, $collection);
        $mongoBase->removeDataBase();
        fwrite(STDOUT, "removeDataBase[{$db}] succeed\n");
    }

    public function doRemoveCollection()
    {
        global $argv;
        if (count($argv) < 4) {
            $this->usage($argv[0]);
            return;
        }
        $db = $argv[2];
        $collection = $argv[3];
        $mongoBase = new MongoDbBase($db, $collection);
        $mongoBase->delete([]);
        fwrite(STDOUT, "doRemoveCollection[{$db}.{$collection}] succeed\n");
    }
}

$method = null;
if (count($argv) > 1) {
    $method = $argv[1];
}
$app->run($method);
